﻿using MongoDB.Driver;
using Sustainalytics.Entities.RiskRatings;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace Sustainalytics.RiskRatings.Service.Controllers.Company
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/v1/companies")]
    public class HistogramController : ApiController
    {
        private readonly IMongoCollection<PeerAggregate> _peerAggregateCollection;


        public HistogramController(IMongoCollection<PeerAggregate> collection)
        {
            _peerAggregateCollection = collection;
        }


        [HttpGet]
        [Route("benchmark")]
        public HttpResponseMessage GetRiskRatingDistribution()
        {
            var data = _peerAggregateCollection.AsQueryable().OfType<UniverseAggregate>().FirstOrDefault().Data;

            return Request.CreateResponse(HttpStatusCode.OK, data);
        }

        [HttpGet]
        [Route("benchmark/subindustry")]
        public HttpResponseMessage GetRiskRatingSubindustryDistribution([FromUri] int SubIndustryId)
        {
            var group = _peerAggregateCollection.AsQueryable().OfType<SubIndustryAggregate>().FirstOrDefault(x=>x.SubIndustryId == SubIndustryId);

            if (group==null)
            {
                Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            return Request.CreateResponse(HttpStatusCode.OK, group.Data);
        }
    }
}
